package com.jsyl.lwbk.business.service.impl;

import com.jsyl.lwbk.business.domain.education.ChildReport;
import com.jsyl.lwbk.business.dto.*;
import com.jsyl.lwbk.business.mapper.education.ChildMonthPlanMapper;
import com.jsyl.lwbk.business.service.ChildMonthPlanService;
import com.jsyl.lwbk.system.utils.DictUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Fitch
 * 2021/9/23
 */
@Service
public class ChildMonthPlanServiceImpl implements ChildMonthPlanService {

    @Autowired
    ChildMonthPlanMapper childMonthPlanMapper;

    @Override
    public List<ChildReportPageDetailDTO> listByType(Long childId, Integer reportType, Integer projectType) {
        ChildReport query = new ChildReport();
        query.setReportType(reportType);
        query.setProjectType(projectType);
        query.setChildId(childId);
        List<ChildReport> list = this.childMonthPlanMapper.page(query);
        List<ChildReportPageDetailDTO> tempList = new ArrayList<>();
        for(ChildReport c:list){
            ChildReportPageDetailDTO dto =new ChildReportPageDetailDTO();
            dto.setId(c.getId());
            dto.setHasRead(c.getHasRead());
            dto.setReportType(c.getReportType());
            // reportType:1-月度计划，2-月训练小结，3-课程报告，4-测评报告
            if(c.getReportType().equals(1)){
                dto.setCenterName(c.getCenterName());
                String projectName = DictUtils.getDictLabel("business_project_type", c.getProjectType().toString()).replaceAll("\r", "");
                dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1] + "月");
                dto.setReportName(projectName + "-月度计划");
                dto.setTeacherName(c.getUpdateBy());
            }

            if(c.getReportType().equals(2)){
                dto.setCenterName(c.getCenterNames());
                dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1]+ "月");
                dto.setReportName("月度小结");
            }

            if(c.getReportType().equals(3)){
                dto.setCenterName(c.getCenterName());
                String projectName = DictUtils.getDictLabel("business_project_type", c.getProjectType().toString()).replaceAll("\r", "");
                dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1] + "月"+c.getTrainingProgramNumber().split("-")[2] + "日");
                dto.setReportName(projectName + "-训练报告");
                dto.setTeacherName(c.getUpdateBy());
            }

            if(c.getReportType().equals(4)){
                dto.setCenterName(c.getCenterName());
                dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1] + "月" +c.getTrainingProgramNumber().split("-")[2] + "日");
                dto.setReportName( "儿童能力发展水平测评分析报告");
                dto.setTeacherName(c.getUpdateBy());
            }
            tempList.add(dto);
        }
        return tempList;
    }

    @Override
    public ChildReportPage listChildReport(Long childId, Integer currentPage, Integer pageSize) {
        ChildReport query = new ChildReport();
        query.setPageSize(pageSize);
        query.setCurrentPage((currentPage-1) * pageSize);
        query.setChildId(childId);
        List<ChildReport> list = this.childMonthPlanMapper.page(query);
        ChildReportPage reportPage = new ChildReportPage();
        if (CollectionUtils.isEmpty(list)) {
            reportPage.setTotal(0);
        }else {
            reportPage.setTotal(this.childMonthPlanMapper.pageCount(query));
            List<ChildReportPageDetailDTO> tempList = new ArrayList<>();
            for(ChildReport c:list){
                ChildReportPageDetailDTO dto =new ChildReportPageDetailDTO();
                dto.setId(c.getId());
                dto.setHasRead(c.getHasRead());
                dto.setReportType(c.getReportType());
                dto.setProjectType(c.getProjectType());
                // reportType:1-月度计划，2-月训练小结，3-课程报告，4-测评报告
                if(c.getReportType().equals(1)){
                    dto.setCenterName(c.getCenterName());
                    String projectName = DictUtils.getDictLabel("business_project_type", c.getProjectType().toString()).replaceAll("\r", "");
                    dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1] + "月");
                    dto.setReportName(projectName + "-月度计划");
                    dto.setTeacherName(c.getUpdateBy());
                }

                if(c.getReportType().equals(2)){
                    dto.setCenterName(c.getCenterNames());
                    dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1]+ "月");
                    dto.setReportName("月度小结");
                }

                if(c.getReportType().equals(3)){
                    dto.setCenterName(c.getCenterName());
                    String projectName = DictUtils.getDictLabel("business_project_type", c.getProjectType().toString()).replaceAll("\r", "");
                    dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1] + "月"+c.getTrainingProgramNumber().split("-")[2] + "日");
                    dto.setReportName(projectName + "-训练报告");
                    dto.setTeacherName(c.getUpdateBy());
                }

                if(c.getReportType().equals(4)){
                    dto.setCenterName(c.getCenterName());
                    dto.setReportDate(c.getTrainingProgramNumber().split("-")[0] + "年" + c.getTrainingProgramNumber().split("-")[1] + "月" +c.getTrainingProgramNumber().split("-")[2] + "日");
                    dto.setReportName( "儿童能力发展水平测评分析报告");
                    dto.setTeacherName(c.getUpdateBy());
                }
                tempList.add(dto);
            }
            reportPage.setList(tempList);
        }
        return reportPage;
    }

    /**
     * 获取月度计划内容
     *
     * @param id
     * @return
     */
    @Override
    public ChildMonthPlanDTO getChildMonthPlan(Long id) {
        List<ChildMonthPlanVO> list = this.childMonthPlanMapper.getById(id);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        ChildMonthPlanDTO result = new ChildMonthPlanDTO();
        List<ChildMonthPlanDetailDTO> childMonthPlanDetailDTOs = new ArrayList<ChildMonthPlanDetailDTO>();
        for (ChildMonthPlanVO vo : list) {
            result.setChildId(vo.getChildId());
            result.setChildName(vo.getChildName());
            String projectName = DictUtils.getDictLabel("business_project_type", vo.getProjectType()).replaceAll("\r", "");
            result.setPlanName(vo.getTrainingProgramNumber().split("-")[0] + "年" + vo.getTrainingProgramNumber().split("-")[1] + "月" + projectName + "月度计划");
            if(vo.getTrainingType().equals("3")){
                result.setPlanName(vo.getTrainingProgramNumber().split("-")[0] + "年" + vo.getTrainingProgramNumber().split("-")[1] + "月" + projectName + "标准计划");
            }
            result.setTeacherName(vo.getTeacherName());

            ChildMonthPlanDetailDTO detailDTO = new ChildMonthPlanDetailDTO();
            if(vo.getTrainingModule()!=null){
                detailDTO.setModuleName(DictUtils.getDictLabel("business_training_module", vo.getTrainingModule()).replaceAll("\r", "") + "训练");
            }
            if(vo.getTrainingModule().equals("30")){
                detailDTO.setModuleName("课堂引入项目");
            }
            if(vo.getTrainingModule().equals("40")){
                detailDTO.setModuleName("正式课堂内容");
            }
            if(vo.getTrainingModule().equals("50")){
                detailDTO.setModuleName("课程收尾");
            }
            if (vo.getTrainingContent() != null) {
                List<String> contentList = Arrays.asList(vo.getTrainingContent().split("\\|"));
                detailDTO.setTrainingNames(contentList);
            }
            childMonthPlanDetailDTOs.add(detailDTO);
        }
        result.setList(childMonthPlanDetailDTOs);
        return result;
    }

    @Override
    public List<Map> getScheduleReportIds(ChildScheduleReportDTO dto) {
        List<Map> result =new ArrayList<>();
        List<ChildReport> list =this.childMonthPlanMapper.getScheduleReport(dto.getChildIds(),dto.getDayDates());
        for(ChildReport c:list){
            Map temp =new HashMap();
            temp.put("courseId",c.getCourseId());
            temp.put("childId",c.getChildId());
            temp.put("id",c.getId());
            temp.put("reportType",c.getReportType());
            result.add(temp);
        }
        return result;
    }
}
